package com.ccys.db.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.db.entity.TblBooking;
import com.ccys.db.entity.TblBookingFee;
import com.ccys.db.entity.TblBookingGrab;
import com.ccys.db.entity.TblBookingPatientAddress;
import com.ccys.db.mapper.TblBookingFeeMapper;
import com.ccys.db.mapper.TblBookingGrabMapper;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.mapper.TblBookingPatientAddressMapper;
import com.ccys.db.service.BookingBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: BookingBasicServiceImpl
 * @Description:
 * @Date: 2024/2/22 10:46
 */
@Service
@Slf4j
public class BookingBasicServiceImpl implements BookingBasicService {

    @Resource
    private TblBookingMapper bookingMapper;

    @Resource
    private TblBookingFeeMapper bookingFeeMapper;

    @Resource
    private TblBookingPatientAddressMapper bookingPatientAddressMapper;

    @Resource
    private TblBookingGrabMapper bookingGrabMapper;

    @Override
    public String getStatusByBooking(String bookingId) {

        Integer status;
        Integer subStatus;

        /**
         * todo 这两个字段需要从订单表取
         * delivery_type 1 is seft_collect
         * delivery_type 2 is deliver
         * express_type = 0 is Inhouse driver
         * express_type = 1 is Grab
         * */
        Integer deliveryType = null;
        Integer expressType = null;

        Integer grabStatus = null;
        String patientCountry;

        TblBooking booking = bookingMapper.selectByPrimaryKey(bookingId);
        status = booking.getStatus();
        subStatus = booking.getSubStatus();

        Example example = new Example(TblBookingFee.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bookingId", booking);
        example.setOrderByClause("created_on_utc");
        TblBookingFee fee = bookingFeeMapper.selectByExample(example).get(0);

        if (expressType == 1) {
            example = new Example(TblBookingGrab.class);
            criteria = example.createCriteria();
            criteria.andEqualTo("bookingId", booking);
            example.setOrderByClause("created_on_utc desc");
            List<TblBookingGrab> grabList = bookingGrabMapper.selectByExample(example);
            if (!grabList.isEmpty()) {
                grabStatus = grabList.get(0).getStatus();
            }
        }

        example = new Example(TblBookingPatientAddress.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("bookingId", booking);
        TblBookingPatientAddress address = bookingPatientAddressMapper.selectOneByExample(example);
        patientCountry = address.getCountry();

        String result = "";
        //判断状态内容
        if (deliveryType == 2 || deliveryType == 3) {
            if (status == -5) {
                result = "Invalid QR code";
            } else if (status == -4) {
                result = "Invalid ID";
            } else if (status == -3) {
                result = "Patient not home";
            } else if (status == -2) {
                result = "Patient uncontactable";
            }
        } else if (status == -1) {
            if (subStatus == -8) {
                result = "GoApotik Cancelled";
            } else {
                result = "Cancelled";
            }
        } else if (status == 1 && subStatus == -2) {
            result = "Draft";
        } else if (status == 1) {
            result = "Pending";
        } else if (status == 2) {
            result = "Ready";
        } else if (status == 3) {
            result = "Consulting";
        } else if (status == 4) {
            if ("245".equals(patientCountry) && deliveryType > 0) {
                if (subStatus == -3) {
                    //todo 缺少1.0中的判断fn_Check_Deliver_Is_Cash_Method[bookingId] = 1
                    if (deliveryType == 1 || ((deliveryType == 2 || deliveryType == 3)/*&&*/)) {
                        result = "Pending Collection";
                    } else {
                        result = "Unpaid";
                    }
                } else if (subStatus == -4) {
                    result = "Paid";
                }
            } else {
                if (subStatus == -3) {
                    result = "Unpaid";
                } else {
                    result = "Consulted";
                }
            }
        } else if (status == 5) {
            if ("245".equals(patientCountry)) {
                if (deliveryType == 2 || deliveryType == 3) {
                    result = "Paid";
                } else {
                    result = "Completed";
                }
            } else {
                result = "Paid";
            }
        } else if (status == 6) {
            if (expressType == 1) {
                //grab
                if (grabStatus != null && grabStatus == 1) {
                    result = "Scheduling";
                } else if (grabStatus != null && grabStatus == 6) {
                    result = "Scheduling Failed";
                }
            } else {
                if (deliveryType == 1) {
                    //self collect
                    result = "For collection";
                } else if (deliveryType == 2 || deliveryType == 3) {
                    //delivery
                    if (subStatus == 2) {
                        result = "Failed";
                    } else {
                        result = "Packed";
                    }
                }
            }
        } else if (status == 7) {
            if (deliveryType == 1) {
                //self collect
                result = "Uncollected";
            } else if (deliveryType == 2 || deliveryType == 3 || deliveryType == 4) {
                //delivery
                result = "Pending collection";
            }
        } else if (status == 8) {
            if (deliveryType == 2 || deliveryType == 3) {
                //delivery
                result = "Delivering";
            }
        } else if (status == 99) {
            if ("245".equals(patientCountry)) {
                result = "Completed";
            } else if (deliveryType == 1 || deliveryType == null || deliveryType == 0) {
                //self collect
                result = "Consulted";
            } else if (deliveryType == 2 || deliveryType == 3) {
                //delivery
                result = "Completed";
            }
        } else if (status == -10) {
            if (subStatus == -6) {
                result = "Package damaged";
            } else if (subStatus == -7) {
                result = "Package lost";
            } else if (subStatus == -8) {
                result = "Vehicle breakdown";
            } else if (subStatus == -9) {
                result = "Vehicle accident";
            } else if (subStatus == -10) {
                result = "Wrong address";
            }
        } else if (status == -20) {
            if (subStatus == -1) {
                result = "Completed - Patient app issue";
            } else if (subStatus == -2) {
                result = "Completed - Webpage issue";
            }
        } else if (status == 98) {
            if (subStatus == 1) {
                result = "Completed - WhiteCoat Verified";
            } else if (subStatus == 2) {
                result = "Completed - Grab Verified";
            }
        } else if (status == 0) {
            if (subStatus == 0) {
                result = "Canceled";
            } else if (subStatus == 1) {
                result = "Allocating Driver";
            }
        } else if (expressType == 1 && status == null) {
            if (grabStatus != null && grabStatus == 6) {
                result = "Scheduling Failed";
            } else if (subStatus == null) {
                result = "Scheduling";
            }
        } else if (status == -6) {
            if (subStatus == 1) {
                result = "Lapsed";
            } else {
                result = "Disconnected";
            }
        }

        return result;
    }

    @Override
    public String getSubStatusByBooking(String bookingId) {
        TblBooking booking = bookingMapper.selectByPrimaryKey(bookingId);
        Integer status = booking.getStatus();
        Integer subStatus = booking.getSubStatus();
        String result = "";
        if (status == -1) {
            if (subStatus == 0) {
                result = "Canceled by patient";
            } else if (subStatus == -1) {
                result = "Canceled by doctor";
            }
        } else if (status == 1) {
            if (subStatus == -1) {
                result = "Skip by doctor";
            } else if (subStatus == -7) {
                result = "Skip by doctor";
            }
        } else if (status == 3) {
            if (subStatus == 1) {
                result = "Callended";
            }
        } else if ((status == 2 || status == 3) && subStatus == -1) {
            result = "Skip by doctor";
        } else if (status == 5) {
            if (subStatus == -1) {
                result = "Reset medication by Pharmacist";
            } else if (subStatus == -2) {
                result = "Pending partial paid";
            } else if (subStatus == -3) {
                result = "Partial paid prescription";
            } else if (subStatus == 1) {
                result = "Pharmacist has scanned QR code";
            } else if (subStatus == 2) {
                result = "Pharmacist has confirmed IC valid";
            } else if (subStatus == 3) {
                result = "Pharmacist marked booking as partial completed";
            }
        } else if (status == 7) {
            switch (subStatus) {
                case 1:
                    result = "Driver assigned";
                    break;
                case 2:
                    result = "Patient contacted";
                    break;
                case 3:
                    result = "Pharmacist has scanned Driver QR code";
                    break;
                case 4:
                    result = "Pharmacist has confirmed Driver IC valid";
                    break;
                case 5:
                    result = "Pharmacist has completed collection";
                    break;
                default:
                    break;
            }
        } else if (status == 9 && subStatus == 1) {
            result = "Driver has scanned QR code";
        }

        return result;
    }

    /**
     * 检查booking是否处于问诊中
     *
     * @param bookingId bookingId
     * @return
     */
    @Override
    public Boolean isInConsultation(String bookingId) {
        TblBooking booking = bookingMapper.selectByPrimaryKey(bookingId);
        if (ObjectUtil.isNull(booking) || booking.getDelFlag() == 1) {
            return false;
        }
//        t1.del_flag = 0
//        and ((t1.status = 4 and t1.doctor_is_completed = 0) or t1.status = 3 or (t1.status = 2 and t1.sub_status = 0 and t1.consultation_mode = 5))
        if (booking.getStatus() == 4 && booking.getDoctorIsCompleted() == 0) {
            return true;
        } else if (booking.getStatus() == 3) {
            return true;
        } else if (booking.getStatus() == 2 && booking.getSubStatus() == 0 && booking.getConsultationMode() == 5) {
            return true;
        }
        return false;
    }
}
